﻿using EMS.system.Common;
using EMS.system.Entityies.MasterData.WorkstationModel;
using EMS.system.Entityies.ProductionManagement.ProductionWorkOrderModel;
using EMS.system.Entityies.WarehouseManagement.ProductionRequisitionModel;
using EMS.system.Entityies.WarehouseManagement.WarehouseSettingsModel;
using EMS.system.IEntityAppService.WarehouseManagement.MaterialRequisition;
using EMS.system.IEntityAppService.WarehouseManagement.WarehouseSetup;
using EMS.system.IEntityAppService.WarehouseManagement.WarehouseSetup.WarehouseSetupDTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace EMS.system.EntityAppService.WarehouseManagement.MaterialRequisition
{
    public class ProductionRequisitionAppService : systemAppService, IProductionRequisitionAppService
    {
        private readonly IRepository<ProductionRequisition, int> _repository;
        private readonly IRepository<ProductionWorkOrder, int> _PwoRepository;
        private readonly IRepository<Workstation, int> _WorkRepository;

        public ProductionRequisitionAppService(IRepository<ProductionRequisition, int> repository, IRepository<ProductionWorkOrder, int> pwoRepository, IRepository<Workstation, int> workRepository)
        {
            _repository = repository;
            _PwoRepository = pwoRepository;
            _WorkRepository = workRepository;
        }
        public async Task<int> AddProductionRequisition(SalesReturnsDTO dto)
        {
            var productionRequisition = dto.MapTo<ProductionRequisition>();
            await _repository.InsertAsync(productionRequisition);
            return 1;
        }

        public PagedResult GetProductionRequisition(string? code, string? name, int page = 1, int limit = 2)
        {
            var predicate = PredicateBuilder.New<ProductionRequisition>(true);
            if (!string.IsNullOrEmpty(code))
            {
                predicate = predicate.And(m => m.RequisitionCode.Contains(code));
            }
            if (!string.IsNullOrEmpty(name))
            {
                predicate = predicate.And(m => m.RequisitionName.Contains(name));
            }
            var dto = _repository.GetListAsync(predicate).Result;
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / limit);
            result.PageSize = limit;
            result.CurrentPage = page;
            result.RowCount = dto.Count;
            result.Queryable = dto.Skip((page - 1) * limit).Take(limit).AsQueryable();
            return result;
        }

        public async Task<int> DeleteProductionRequisition(int id)
        {
            await _repository.DeleteAsync(id);
            return 1;
            
        }

        public async Task<ProductionRequisition> GetProductionRequisitionById(int id)
        {
            var model =  await _repository.FirstOrDefaultAsync(x => x.Id == id);
            return model;
        }
        public async Task<int> UpdateProductionRequisition(SalesReturnsDTO dto)
        {
            var model = dto.MapTo<ProductionRequisition>();
            await _repository.UpdateAsync(model);
            return 1;
        }
        public List<ProductionWorkOrder> GetProductionWorkOrder()
        {
            var list = _PwoRepository.GetListAsync().Result;
            return list.ToList();
        }
        public List<Workstation> GetWorkstation()
        {
            var list = _WorkRepository.GetListAsync().Result;
            return list.ToList();
        }
    }
}
